package com.remind;

/**
 * ClassName: test
 * Description:
 *
 * @Author liu
 * @Create 2024/8/13 10:19
 * @Version 1.0
 */

public class test {
    public static void main(String[] args) {

        //1.  数值型数组的特征值的计算：最大值、最小值、总和、平均值等
        int[] arr =new int[]{1,3,6,7,8,9};
        int max =arr[0];
        int min =arr[0];
        int sum =0;
        int div =0;
        for (int i = 0; i <arr.length ; i++) {
            sum +=arr[i];
            if(max < arr[i]){
                max =arr[i];
            }
            if(min > arr[i]){
                min =arr[i];
            }
        }
        div = sum/arr.length;
        System.out.println(max);
        System.out.println(min);
        System.out.println(sum);
        System.out.println(div);



        //2. 数组元素的赋值。比如：杨辉三角；彩票随机生成数（6位；1-30；不能重复）；回形数
        //杨辉三角(10行)
//        1
//        1 1
//        1 2 1
//        1 3 3 1
//        1 4 6 4 1
//    j[] 0 1 2 3 4
        int[][] yanghui = new int[10][];//null
        for (int i = 0; i < yanghui.length; i++) {
            // 每一行包含的元素数量等于行索引 i 加一
            yanghui[i] = new int[i + 1];
            //第一行与最后一行为1
            yanghui[i][0] = 1;
            yanghui[i][i] = 1;
            for (int j = 1; j < yanghui[i].length - 1; j++) {
                yanghui[i][j] = yanghui[i - 1][j] + yanghui[i - 1][j - 1];
            }

        }
        //遍历
        for (int i = 0; i < yanghui.length; i++) {
            for (int j = 0; j < yanghui[i].length; j++) {
                System.out.print(yanghui[i][j] + " ");

            }
            System.out.println();
        }

    }




        //3. 数组的反转
        int[] arr =new int[]{1,2,3,4,5,6,7,8};
        //法一：用中间判断次数 arr.length/2
        //遍历
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]);
        }
       for (int i = 0; i <arr.length/2 ; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] =temp;

        }
        System.out.println();
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]);
        }

        //法二：用左右向中间换位 left <right
        for (int left = 0,right =arr.length-1; left <right ; left++,right--) {
            int temp =arr[left];
            arr[left] =arr[right];
            arr[right] = temp;

        }
        System.out.println();
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]);
        }



        // 4. 数组的扩容、缩容
        //扩容 一倍 加入 44 55 66
        int[] arr = new int[]{1,3,4,5,6,77,76};
        //用新数组代换旧的，再换回来
        int[] newarr=new int[arr.length << 1];
        for (int i = 0; i <arr.length ; i++) {//*2
            newarr[i] = arr[i];
        }
        newarr[arr.length]=44;
        newarr[arr.length+1]=55;
        newarr[arr.length+2]=66;
        arr =newarr;
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]+" ");

        }
        //缩容
        public class arrary_test2 {
            public static void main(String[] args) {
                //不建数组

        int[] arr={1,2,3,4,5,6,7};
        int indent = 4;
        for (int i = indent; i <arr.length-1 ; i++) {
            arr[i] =arr[i +1];
        }
        arr[arr.length-1]=0;
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]+" ");

        }

                //建数组
                int[] arr={1,2,3,4,5,6,7};
                int index = 4;
                int[] newArr = new int[arr.length - 1];
                for (int i = 0; i < index; i++) {
                    newArr[i] = arr[i];
                }

                for(int i = index;i < arr.length - 1;i++){
                    newArr[i] = arr[i + 1];
                }

                arr = newArr;
                for (int i = 0; i <arr.length ; i++) {
                    System.out.print(arr[i]+" ");

                }


            }

        }
    }





// 数组的查找
    //线性查找
public class arrary_xianxichazhao {
    public static void main(String[] args) {
        int[] arr1 = new int[]{34,54,3,2,65,7,34,5,76,34,67};
        //查找元素5是否在上述数组中出现过
        boolean flag = true;
        int num = 87;
        for (int i = 0; i <arr1.length ; i++) {
            if(num == arr1[i]){
                System.out.print("找到了，"+"在第"+i+"位");
                flag = false;
                break;
            }

        }
        if(flag) {
            System.out.print("抱歉，没有该数");
        }

    }
}


//  - 二分法查找（前提：数组有序）
//二分法查找 ==>递增或递减
//1.定义目标元素
//2.定义首，尾，找到中间数
//3判断目标元素与中间大小，
//4 大了；首=中+1；小了：尾=中-1
public class twofeng {
    public static void main(String[] args) {
        int[] arr2 = new int[]{2,4,5,8,12,15,19,26,37,49,51,66,89,100};
        int target = 5;
        boolean flag = true;

        int head = 0;
        int end = arr2.length-1;
        while(head<end) {
            int mid = (head + end)/2;
            if (target == arr2[mid]) {
                System.out.print("找到了，" + "在第" + mid + "位");
                flag = false;
                break;
            } else if (target > arr2[mid]) {
                head = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        if (flag) {
            System.out.print("抱歉，没有该数");
        }
    }
}



// 数组的排序
//  - 冒泡排序：最简单
public class maopao {
    public static void main(String[] args) {
        int[] arr = new int[]{34,54,3,2,65,7,34,5,76,34,67};
        //遍历
        for (int i = 0; i <arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        // 使用冒泡排序算法对数组进行排序
        // 外层循环控制排序轮数，每轮找到一个最大元素的位置
        for (int i = 0; i < arr.length - 1; i++) {
            // 内层循环用于比较相邻元素并进行交换
            // 随着每轮排序完成，未排序部分的长度减少，因此循环次数也相应减少
            for (int j = 0; j < arr.length - 1 - i; j++) {
                // 如果当前元素大于下一个元素，则交换它们的位置
                // 这样可以确保每次内层循环结束后，未排序部分中的最大元素被放置在正确的位置
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        System.out.println();
        for (int i = 0; i <arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}


}

